package com.yunxi.service.dubboService.impl.sto;

import com.alibaba.cloud.context.utils.StringUtils;
import com.esotericsoftware.minlog.Log;
import com.github.pagehelper.PageInfo;
import com.yunxi.model.sto.StoWorkDay;
import com.yunxi.service.localService.sto.StoWorkDayService;
import com.yunxi.sto.pojo.StoWorkDayVO;
import com.yunxi.sto.service.StoWorkDayDubboService;
import com.yunxi.util.S2SEnums.WorkStatusType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 人员排班表 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-08-15
 */
public class StoWorkDayDubboServiceImpl implements StoWorkDayDubboService {

    @Autowired
    @Qualifier(value = "stoWorkDayServiceImpl")
    StoWorkDayService stoWorkDayService;


    @Override
    public StoWorkDayVO getById(String id) throws Exception {
        StoWorkDay stoWorkDay = stoWorkDayService.getById(id);
        if(stoWorkDay == null)throw new Exception("操作失败,无法根据id:"+id+"找到对应记录!");
        StoWorkDayVO stoWorkDayVO = new StoWorkDayVO();
        BeanUtils.copyProperties(stoWorkDay, stoWorkDayVO);
        return stoWorkDayVO;
    }

    @Override
    public void saveOrUpdate(StoWorkDayVO stoWorkDayVO) throws Exception {
        Date date = new Date();
        if(stoWorkDayVO.getWorkDayId() == null || StringUtils.isEmpty(stoWorkDayVO.getWorkDayId())){
            stoWorkDayVO.setWorkMonth(date);
            stoWorkDayVO.setWorkDayId(null);
        }
        else {
            StoWorkDay stoWorkDay = stoWorkDayService.getById(stoWorkDayVO.getWorkDayId());
            if(stoWorkDay != null){
                Date workMonth = stoWorkDay.getWorkMonth();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
                String format = simpleDateFormat.format(workMonth);
                String format1 = simpleDateFormat.format(date);
                if(!format.equals(format1)){
                    stoWorkDayVO.setWorkDayId(null);
                    stoWorkDayVO.setWorkMonth(date);
                }
            }
        }
        StoWorkDay stoWorkDay = new StoWorkDay();
        BeanUtils.copyProperties(stoWorkDayVO, stoWorkDay);
       boolean a = stoWorkDayService.saveOrUpdate(stoWorkDay);

    }

    @Override
    public void removeById(String id) {
        stoWorkDayService.removeById(id);
    }

    @Override
    public PageInfo<StoWorkDayVO> findPageByVO(StoWorkDayVO stoWorkDayVO, Integer pageNum, Integer pageSize) throws Exception {
        PageInfo<StoWorkDayVO> pageInfo = stoWorkDayService.pageByVo(stoWorkDayVO, pageNum, pageSize);
        List<StoWorkDayVO> list = pageInfo.getList();
        if (list != null && list.size() > 0){
            for (StoWorkDayVO vo : list){
                String workDays = vo.getWorkDays();
                if(workDays==null){
                    workDays="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31";
                }
                if(StringUtils.isNotEmpty(workDays)){
                    String[] split = workDays.split(",");
                    List<String> list1 = Arrays.asList(split);
                    vo.setWorkDayList(list1);
                }
            }
        }
        return pageInfo;
    }

    @Override
    public void removeByListVo(List<StoWorkDayVO> stoWorkDayVOList) throws Exception {
        stoWorkDayService.removeByListVo(stoWorkDayVOList);
    }

    @Override
    public List listByVo(StoWorkDayVO stoWorkDayVO) {
        return stoWorkDayService.listByVo(stoWorkDayVO);
    }
}
